//
//  SelectionViewController.m
//  Project 3
//
//  Created by mac on 16/7/14.
//  Copyright © 2016年 www. All rights reserved.
//

#import "SelectionViewController.h"
#import "SlideUnlimitedView.h"
#import "TableViewCell.h"
#import "NetworkRequest.h"
#define kScreenWidth [UIScreen mainScreen].self.bounds.size.width
#define kScreenHeight [UIScreen mainScreen].self.bounds.size.height
@interface SelectionViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation SelectionViewController{
    
    NSMutableArray *_mArray;
    
    UITableView *_tableView;
    
    SlideUnlimitedView *_slideView;
    
    NSArray *_array;
}

static NSString *cellID = @"cellID";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //请求数据
    [self _NetworkRequest];
    
    _mArray = [NSMutableArray array];
    
    //分组表视图
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    //注册单元格
    [_tableView registerClass:[TableViewCell class] forCellReuseIdentifier:cellID];
    
    UIView *hearView = [self _createHeaderView];
    
    _tableView.tableHeaderView = hearView;
    
    [self.view addSubview:_tableView];
    
    
    //委托代理
    _tableView.delegate = self;
    
    //数据源代理
    _tableView.dataSource = self;
    
    //创建导航栏
    [self createNavigation];

}

- (void)_NetworkRequest{
    
    [NetworkRequest requestWithURL:@"http://api.daydaycook.com.cn/daydaycook/server/ad/listAds.do?languageId=3&mainland=1&deviceId=6A7DDB26-7537-4DB2-8851-7C7B7ED0F75C&uid=141123&regionCode=156" httpMethod:nil params:nil fileData:nil success:^(id result) {
        
        NSDictionary *dic = result;
        
        _array = dic[@"data"];
        
        
        
        for (NSDictionary *dataDic in _array) {
            
            NSString *str = dataDic[@"path"];
            
            [_mArray addObject:str];
            
        }
        
        _slideView.imageArray = _mArray;
        
        _tableView.tableHeaderView = _slideView;
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    

}

//无限滑动头视图
- (UIView *)_createHeaderView {
    
//    NSMutableArray *array = [NSMutableArray array];
//    
//    for (int i = 1; i <= 5; i++) {
//        
//        NSString *str = [NSString stringWithFormat:@"slide%d.jpeg",i];
//        
//        UIImage *image = [UIImage imageNamed:str];
//        
//        [array addObject:image];
//    }
    
    
    _slideView =[[SlideUnlimitedView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
//
//    _slideView.imageArray = _mArray;
    
    
    return  _slideView;
    
}

//导航栏中间图片和右边button
- (void)createNavigation{
    
    //设置右边的导航项----button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(0, 0, 30, 30);
    
    [button setImage:[UIImage imageNamed:@"image1.jpg"] forState:UIControlStateNormal];
    
    //button点击事件
    //[button addTarget:self action:@selector(rightBarButtonItemAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    
    //设置中间的导航项----image
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
    
    imgView.image = [UIImage imageNamed:@"image2.jpg"];
    
    self.navigationItem.titleView = imgView;
}

//多少个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 4;
}

//每组多少个
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 2) {
        
        return 4;
    }else{
        
        return 1;
    }
}

//定制单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    
    [cell.contentView addSubview:imageView];
    
    //上面大字
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.textColor = [UIColor whiteColor];
    
    label.font = [UIFont boldSystemFontOfSize:20];
    
    [imageView addSubview:label];
    
    //下面小字
    UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 120, kScreenWidth, 40)];
    
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    
    bottomLabel.textColor = [UIColor whiteColor];
    
    bottomLabel.font = [UIFont boldSystemFontOfSize:16];
    
    [imageView addSubview:bottomLabel];
    
    if (indexPath.section == 0) {
        
        imageView.image = [UIImage imageNamed:@"image1.jpg"];
        
    }else if (indexPath.section == 1){
        
        imageView.image = [UIImage imageNamed:@"image2.jpg"];
        
    }else if (indexPath.section == 2){
        
        if (indexPath.row == 0) {
            
            
            imageView.image = [UIImage imageNamed:@"theme1.jpeg"];
            label.text = @"有一处吃货天堂叫七宝";
        }else if (indexPath.row == 1){
            
            imageView.image = [UIImage imageNamed:@"theme2.jpeg"];
            label.text = @"爱做饭的男人智商高";
        }else if (indexPath.row == 2){
            
            imageView.image = [UIImage imageNamed:@"theme3.jpeg"];
            label.text = @"请把夏天让给西瓜";
            bottomLabel.text = @"5道美味煮物推荐";
        }else if (indexPath.row == 3){
            
            imageView.image = [UIImage imageNamed:@"theme4.jpeg"];
            label.text = @"解暑必备甜蜜西米糖水";
            bottomLabel.text = @"5道美味煮物推荐";
        }

        
    }else if (indexPath.section == 3){
        
            imageView.image = [UIImage imageNamed:@"image3.jpg"];
    }

    
    return cell;
}

//单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200;
}

//分组头视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        
    return 50;
}

//分组尾视图高度 (默认有,给0.01 相当于相除)
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}

//分组头视图 (图+文)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section{
    
    static NSString *headerID = @"headerID";

//    如果要设置分组头视图的重用,则必须使用系统指定类,也是一个视图
//    从闲置池中取出可重用的分组头视图
    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
    
    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:headerID];
    
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 15, 25 ,25)];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(180, 15, 100, 25)];
        
        if (section == 0) {
            
            imgView.image = [UIImage imageNamed:@"image1.jpg"];
            label.text = @"每日新品";
        }else if (section == 1){
            
            imgView.image = [UIImage imageNamed:@"image2.jpg"];
            label.text = @"热门推荐";
        }else if (section == 2){
            
            imgView.image = [UIImage imageNamed:@"image3.jpg"];
            label.text = @"主题推荐";
        }else if (section == 3){
            
            imgView.image = [UIImage imageNamed:@"image4.jpg"];
            label.text = @"话题推荐";
        }
        
        [headerView addSubview:label];
        
        [headerView addSubview:imgView];
    
    }
    
    return headerView;

}












@end
